package com.ornament.app.domain;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.persistence.TypedQuery;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;

@Configurable
@Entity
@Table(name = "loginuser")
public class Loginuser {

	@Column(name = "username", length = 45, unique = true)
    @NotNull
    private String username;

	@Column(name = "password", length = 150)
    @NotNull
    private String password;

	public String getUsername() {
        return username;
    }

	public void setUsername(String username) {
        this.username = username;
    }

	public String getPassword() {
        return password;
    }

	public void setPassword(String password) {
        this.password = password;
    }

	public static TypedQuery<Loginuser> findLoginusersByUsernameAndPasswordEquals(String username, String password) {
        if (username == null || username.length() == 0) throw new IllegalArgumentException("The username argument is required");
        if (password == null || password.length() == 0) throw new IllegalArgumentException("The password argument is required");
        EntityManager em = Loginuser.entityManager();
        TypedQuery<Loginuser> q = em.createQuery("SELECT o FROM Loginuser AS o WHERE o.username = :username AND o.password = :password", Loginuser.class);
        q.setParameter("username", username);
        q.setParameter("password", password);
        return q;
    }

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;

	public Integer getId() {
        return this.id;
    }

	public void setId(Integer id) {
        this.id = id;
    }

	@PersistenceContext
    transient EntityManager entityManager;

	public static final EntityManager entityManager() {
        EntityManager em = new Loginuser().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countLoginusers() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Loginuser o", Long.class).getSingleResult();
    }

	public static List<Loginuser> findAllLoginusers() {
        return entityManager().createQuery("SELECT o FROM Loginuser o", Loginuser.class).getResultList();
    }

	public static Loginuser findLoginuser(Integer id) {
        if (id == null) return null;
        return entityManager().find(Loginuser.class, id);
    }

	public static List<Loginuser> findLoginuserEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Loginuser o", Loginuser.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Loginuser attached = Loginuser.findLoginuser(this.id);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public Loginuser merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Loginuser merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }
}
